package com.ssi.myshop.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.viewpager.widget.ViewPager;

import com.ssi.myshop.Presenter.impl.IAddToCarPresenterImpl;
import com.ssi.myshop.Presenter.impl.IDetailPresenterImpl;
import com.ssi.myshop.R;
import com.ssi.myshop.base.BaseActivity;
import com.ssi.myshop.bean.CategoryContentBean;
import com.ssi.myshop.bean.DetailBean;
import com.ssi.myshop.ui.adapter.DetailLoopPageAdapter;
import com.ssi.myshop.ui.custom.AutoLoopViewPager;
import com.ssi.myshop.utils.PresenterManager;
import com.ssi.myshop.utils.SizeUtils;
import com.ssi.myshop.view.IAddToCarCallBack;
import com.ssi.myshop.view.IDetailCallBack;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;

public class DetailActivity extends BaseActivity implements IDetailCallBack, View.OnClickListener, IAddToCarCallBack {

    private static final String TAG = "DetailActivity";
    private IDetailPresenterImpl mIDetailPresenter;

    private Integer id = null;

    @BindView(R.id.detail_loop_view)
    public AutoLoopViewPager looperPager;

    @BindView(R.id.detail_looper_point_container)
    LinearLayout looperPointContainer;

    @BindView(R.id.my_scrollView)
    ScrollView mScrollView;


    @BindView(R.id.rich_text)
    TextView mTextView;

    @BindView(R.id.tv_add_to_car)
    public TextView tvAdd;

    @BindView(R.id.show_car_view)
    public LinearLayout addToCarView;

    @BindView(R.id.transparent_view)
    public RelativeLayout mTransparentView;


    @BindView(R.id.tv_price)
    public TextView mTvPrice;

    @BindView(R.id.tv_name)
    public TextView mTvName;

//    @BindView(R.id.web_view)
//    WebView mWebView;


    private DetailLoopPageAdapter mDetailLoopPageAdapter;
    private IAddToCarPresenterImpl mIAddToCarPresenter;

    @Override
    protected void initPresent() {
        mIDetailPresenter = PresenterManager.getInstance().getIDetailPresenter();
        mIDetailPresenter.registerViewCallBack(this);
        mIAddToCarPresenter = PresenterManager.getInstance().getIAddToCarPresenter();
        mIAddToCarPresenter.registerViewCallBack(this);
    }

    @Override
    protected void initEvent() {
        if (id != null) {
            mIDetailPresenter.getDetailById(id);
        }
        tvAdd.setOnClickListener(this);
        addToCarView.setOnClickListener(this);
        mTransparentView.setOnClickListener(this);
    }

    @Override
    protected void initView() {
        RichText.initCacheDir(this);
        Intent intent = getIntent();
        CategoryContentBean datail = (CategoryContentBean) intent.getSerializableExtra("datail");
        Log.d(TAG, "initView: " + datail.toString());
        id = datail.getId();
        mDetailLoopPageAdapter = new DetailLoopPageAdapter();
        looperPager.setAdapter(mDetailLoopPageAdapter);
        looperPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mDetailLoopPageAdapter.getDataSize() == 0) {
                    return;
                }
                int targetPosition = position % mDetailLoopPageAdapter.getDataSize();
                updateLooperIndicator(targetPosition);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_detail;
    }

    @Override
    public void onGetDetailSuccess(DetailBean bean) {
        int originalPrice = bean.getBasicInfo().getOriginalPrice();
        mTvPrice.setText("¥" + originalPrice);
        mTvName.setText(bean.getBasicInfo().getName());
        mDetailLoopPageAdapter.setData(bean.getPics());
//        //设置到中间点
        int dx = (Integer.MAX_VALUE / 2) % bean.getPics().size();
        int targetCenterPosition = ((bean.getPics().size() * 100) / 2) - dx;
        looperPager.setCurrentItem(targetCenterPosition);
        looperPointContainer.removeAllViews();
        for (int i = 0; i < bean.getPics().size(); i++) {
            View point = new View(this);
            int size = SizeUtils.dip2px(this, 8);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(size, size);
            layoutParams.leftMargin = SizeUtils.dip2px(this, 5);
            layoutParams.rightMargin = SizeUtils.dip2px(this, 5);
            point.setLayoutParams(layoutParams);
            if (i == 0) {
                point.setBackgroundResource(R.drawable.shape_indacator_point_selected);
            } else {
                point.setBackgroundResource(R.drawable.shape_indacator_point_normal);
            }
            looperPointContainer.addView(point);
        }

//        WebView webView = new WebView(this);
//        webView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        webView.loadDataWithBaseURL("",bean.getContent(),"text/html","utf-8",null);
//        mScrollView.addView(webView);
        RichText.from(bean.getContent()).into(mTextView);
    }

    private void updateLooperIndicator(int targetPosition) {
        for (int i = 0; i < looperPointContainer.getChildCount(); i++) {
            View point = looperPointContainer.getChildAt(i);
            if (i == targetPosition) {
                point.setBackgroundResource(R.drawable.shape_indacator_point_selected);
            } else {
                point.setBackgroundResource(R.drawable.shape_indacator_point_normal);
            }
        }
    }

    @Override
    public void onError(String s) {
//        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onEmpty() {

    }

    @Override
    public void onSuccess() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        looperPager.startLoop();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onStop() {
        super.onStop();
        looperPager.stopLoop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIDetailPresenter.unregisterViewCallBack(this);
        RichText.recycle();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add_to_car:
//                startActivity(new Intent().setClass(this, AddToCarActivity.class));
//                addToCarView.setVisibility(View.VISIBLE);
                mIAddToCarPresenter.addToCar(this,id);
                break;

            case R.id.transparent_view:
                addToCarView.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void addToCarSuccess() {
        Toast.makeText(this, "加入购物车成功", Toast.LENGTH_SHORT).show();
    }
}
